/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package connections;

import java.sql.*;
import java.sql.ResultSet;

/**
 *
 * @author Jeroen
 */
public class Database {

    public static final String SQL_EXCEPTION = "SQL Exception: ";
    private static String userName = "user";
    private static String userPassword = "GTCNKTV6";
    private static String databaseURL = "jdbc:mysql://145.92.6.46:8060/getconnected";
    /*    private static String userName = "root";
    private static String userPassword = "password";
    private static String databaseURL = "jdbc:mysql://localhost:3306/getConnected"; */

    public Connection connect() {
        try {
            Class.forName("com.mysql.jdbc.Driver").newInstance();
            Connection db_connection = DriverManager.getConnection(Database.databaseURL, Database.userName, Database.userPassword);
            return db_connection;
        } catch (SQLException ex) {
            System.err.println("SQLException - " + ex);
        } catch (InstantiationException ex) {
            System.err.println("InstantiationException - " + ex);
        } catch (IllegalAccessException ex) {
            System.err.println("IllegalAccessException - " + ex);
        } catch (ClassNotFoundException ex) {
            System.err.println("ClassNotFoundException - " + ex);
        }
        return null;
    }

    public void close(Connection db_connection) {
        try {
            db_connection.close();
        } catch (SQLException ex) {
            System.err.println("SQLException - " + ex);
        }
    }

    /*
     * Execute the query, with results. Can't UPDATE or ALTER data in de database, only SELECT.
     */

    public ResultSet execute(Connection db_connection, String query) {
        ResultSet result = null;
        try {
            Statement stat = db_connection.createStatement();
            result = stat.executeQuery(query);
        } catch (SQLException ex) {
            System.err.println("SQLException - " + ex);
        }
        return result;
    }

    /*
     * Execute the query, and retrieve the auto generated keys (like auto incrment fields) as a ResultSet object
     */
    public ResultSet insert(Connection db_connection, String query) {
        ResultSet result = null;
        try {
            Statement stat = db_connection.createStatement();
            stat.executeUpdate(query);
            result = stat.getGeneratedKeys();
        } catch (SQLException ex) {
            System.err.println("SQLException - " + ex);
        }
        return result;
    }
    
    /*
     * Execute the query, without results. Used for UPDATE or INSERT statements
     */

    public void executeUpdate(Connection db_connection, String query) {
        try {
            Statement stat = db_connection.createStatement();
            stat.executeUpdate(query);
        } catch (SQLException ex) {
            System.err.println("SQLException - " + ex);
        }
    }
}
